#include "StdAfx.h"

#include <vector>
#include <SFML/Audio.hpp>
#include "../Vocoder/Settings.h"
#include "../Vocoder/SampleMarker.h"
#include "SAPIModulator.h"
#include "VocoderController.h"
#include "TextProvider.h"

VocoderController::VocoderController(void)
{
	sapiModulator = NULL;
	isPlaying = false;
}

VocoderController::~VocoderController(void)
{
}

void VocoderController::Initialize(SAPIModulator* sapiModulator, TextProvider* textProvider, PlayerThread* playerThread)
{
	this->sapiModulator = sapiModulator;
	this->textProvider = textProvider;
	this->playerThread = playerThread;
}

void VocoderController::OnStreamEnded()
{
	if(isPlaying)
		AddCurrentText();
}

SAPIModulator* VocoderController::GetSAPIModulator(){return sapiModulator;}

bool VocoderController::IsPlaying()
{
	return isPlaying;
}

static bool initialized = false;


void VocoderController::PlayOrStop()
{
	if(isPlaying)
	{
		isPlaying = false;
		playerThread->Purge();
		Stop();
		playerThread->Purge();
		Sleep(300);
		playerThread->Purge();
		Stop();
		playerThread->Purge();
	}
	else
	{		
		isPlaying = true;		
		AddCurrentText();
	}
}

void VocoderController::AddCurrentText()
{
	WCHAR* nextText = textProvider->NextText();
	sapiModulator->sapiController.Speak(nextText, false);
}

void VocoderController::Stop()
{
	sapiModulator->sapiController.Speak(NULL, true);
}